/* 
 * File:   VerticalTableHandler.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/VerticalTableHandler.h"

VerticalTableHandler::VerticalTableHandler(string lowImagePath, string highImagePath) throw (NullImageException) {
    try {
        setLowImagePath(lowImagePath);
        setHighImagePath(highImagePath);
    }
    catch(NullImageException &nie) {
        throw nie;
    }
}

VerticalTableHandler::VerticalTableHandler(const VerticalTableHandler& orig) {
}

VerticalTableHandler::~VerticalTableHandler() {
}

/**
 * Sets the original low image path.
 * 
 * @param lowImagePath: original low image path.
 * 
 */
void VerticalTableHandler::setLowImagePath(string lowImagePath) throw (NullImageException) {
    PathValidator* pathValidater = new PathValidator();
    if(pathValidater->validatePathToPages(pathValidater->LOW_PAGE, lowImagePath)) {
        this->originalLowImagePath = lowImagePath;
    }
    else {
        throw NullImageException(lowImagePath);
    }
}

/**
 * Sets the original high image path.
 * 
 * @param highImagePath: original high image path.
 * 
 */
void VerticalTableHandler::setHighImagePath(string highImagePath) throw (NullImageException) {
    PathValidator* pathValidator = new PathValidator();
    if(pathValidator->validatePathToPages(pathValidator->HIGH_PAGE, highImagePath) && 
            pathValidator->validateDotDot(highImagePath)) {
        this->originalHighImagePath = highImagePath;
    }
    else {
        throw NullImageException(highImagePath);
    }
}

/**
 * Create new rotated path.
 * 
 * @param p: path
 * @return path rotated
 */
string VerticalTableHandler::createNewRotatedPath(string p) {
     string newPath = p.replace(p.size()-4, p.size(), "_r");
     newPath += ".png";
//     printf("newPath: %s\n", newPath.c_str());
     return newPath;
}

/**
 * Create new paths to low and high resolution
 * images and rotate both, saving it then.
 * 
 * @return path of new rotated image
 */
vector<string> VerticalTableHandler::rotateImageInNinetyDegrees() throw (NullImageException) {
    vector<string> newPaths;
    
    string newPathHigh = createNewRotatedPath(originalHighImagePath); 
    string newPathLow = createNewRotatedPath(originalLowImagePath);
    
    IplImage* lowImg = cvLoadImage(originalLowImagePath.c_str());
    IplImage* highImg = cvLoadImage(originalHighImagePath.c_str());

    if(lowImg == 0) {
        throw NullImageException(originalLowImagePath);
    }
    else if(highImg == 0) {
        throw NullImageException(originalHighImagePath);
    }
    
    ImageRotor* rotor = new ImageRotor();
    IplImage* rotatedLowImage = rotor->rotatePerpendicularly(lowImg);
    IplImage* rotatedHighImage = rotor->rotatePerpendicularly(highImg);

    cvSaveImage(newPathLow.c_str(), rotatedLowImage);
    cvSaveImage(newPathHigh.c_str(), rotatedHighImage);
    
    newPaths.push_back(newPathLow);
    newPaths.push_back(newPathHigh);
    
    cvReleaseImage(&rotatedLowImage);
    cvReleaseImage(&lowImg);
    cvReleaseImage(&rotatedHighImage);
    cvReleaseImage(&highImg);
    
//    remove(originalLowImagePath.c_str());
//    remove(originalHighImagePath.c_str());
    
    return newPaths;
}


